package com.util;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bean.SystemMessage;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class jPushUtil {
	protected static final Logger LOG = LoggerFactory.getLogger(jPushUtil.class); 
	public static void main(String[] args) {
		JPushClient jpushClient= new JPushClient(Domain.jpush_masterSecret, Domain.jpush_appKey, 3);  
        PushPayload payload=jPushUtil.buildPushObject_ios_tag_alertWithMessage("test", "tag1", "neirong");  
        try {  
            System.out.println(payload.toString());  
            PushResult result = jpushClient.sendPush(payload);  
            System.out.println(result+"................................");
        } catch (APIConnectionException e) {  
            LOG.error("Connection error. Should retry later. ", e);  
        } catch (APIRequestException e) {  
            LOG.error("Error response from JPush server. Should review and fix it. ", e);  
            LOG.info("HTTP Status: " + e.getStatus());  
            LOG.info("Error Code: " + e.getErrorCode());  
            LOG.info("Error Message: " + e.getErrorMessage());  
            LOG.info("Msg ID: " + e.getMsgId()); 
        }
	
	}
	@SuppressWarnings("deprecation")
	public static Map<String, Object> testSendPush(SystemMessage systemMessage,String tag) {  
        Map<String, Object> map=new HashMap<String, Object>();
		JPushClient jpushClient= new JPushClient(Domain.jpush_masterSecret, Domain.jpush_appKey, 3);  
        PushPayload payload=buildPushObject_ios_tag_alertWithMessage(systemMessage.getTitle(), tag, systemMessage.getContent());  
        try {  
            System.out.println(payload.toString());  
            PushResult result = jpushClient.sendPush(payload);  
            System.out.println(result+"................................");
            map.put("code", "0");
            map.put("results", systemMessage);
        } catch (APIConnectionException e) {  
            LOG.error("Connection error. Should retry later. ", e);  
        } catch (APIRequestException e) {  
            LOG.error("Error response from JPush server. Should review and fix it. ", e);  
            LOG.info("HTTP Status: " + e.getStatus());  
            LOG.info("Error Code: " + e.getErrorCode());  
            LOG.info("Error Message: " + e.getErrorMessage());  
            LOG.info("Msg ID: " + e.getMsgId()); 
            map.put("code", e.getStatus());
            map.put("msg", e.getErrorMessage());
        }
        return map;
    }  
	public static PushPayload buildPushObject_ios_tag_alertWithMessage(String alert,String tag,String content) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.all())  
                .setAudience(Audience.newBuilder()  
                        .addAudienceTarget(AudienceTarget.tag(tag))  
                        .build())  
                .setNotification(Notification.newBuilder()  
                        .addPlatformNotification(IosNotification.newBuilder()  
                                .setAlert(alert)  
                                .setBadge(5)  
                                .setSound("happy")  
                                .build())  
                        .build())  
                 .setMessage(Message.content(content))  
                 .setOptions(Options.newBuilder()  
                         .setApnsProduction(true)  
                         .build())  
                 .build();  
    }  
	
}
